package com.egg.ylt.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.GlideUtils;
import com.egg.ylt.pojo.DiaryPhotoEntity;
import com.ruffian.library.widget.RImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * sichaoqi on 2018/12/4
 */
public class ADA_GrowthDiaryRecycler extends RecyclerView.Adapter<ADA_GrowthDiaryRecycler.GrowthViewHolder> {


    private Context mContext;
    private LayoutInflater mInflater;
    private List<DiaryPhotoEntity.ListBean.DateListBean> mDataList;
    private boolean mSelector = false;
    private OnItemClickListener mItemListener;

    public ADA_GrowthDiaryRecycler(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mDataList = new ArrayList<>();
    }

    public void setDataList(List<DiaryPhotoEntity.ListBean.DateListBean> list){
        if (mDataList != null){
            mDataList.clear();
        }
        mDataList.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 获取选择后的数据集合
     * @return
     */
    public List<DiaryPhotoEntity.ListBean.DateListBean> getDataList(){
        return mDataList;
    }

    /**
     * 选择图片
     */
    public void setSelected(boolean selected){
        this.mSelector = selected;
        notifyDataSetChanged();
    }

    /**
     * 取消选择图片
     */
    public void setCancelSelect(){
        for (int i = 0, listLength = mDataList.size(); i < listLength; i++ ){
            DiaryPhotoEntity.ListBean.DateListBean dateListBean = mDataList.get(i);
            dateListBean.setSelected(false);
        }
        setSelected(false);
    }

    @Override
    public GrowthViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new GrowthViewHolder(mInflater.inflate(R.layout.item_growth_recycler, parent, false));
    }

    @Override
    public void onBindViewHolder(final GrowthViewHolder holder, final int position) {
        final DiaryPhotoEntity.ListBean.DateListBean dateListBean = mDataList.get(position);
        //加载图片
        GlideUtils.showImageView(mContext, R.mipmap.ic_growth_default, dateListBean.getThumbnailUrl(), holder.itemIvGrowthPhoto);
        if (mSelector){
        }else{
            //设置当前图片为未选中状态
            holder.itemIvSelected.setVisibility(View.INVISIBLE);
            holder.viewShade.setVisibility(View.INVISIBLE);
        }
        //条目点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelector){
                    if (dateListBean.isSelected()){
                        //设置当前图片为未选中状态
                        dateListBean.setSelected(false);
                        holder.itemIvSelected.setVisibility(View.INVISIBLE);
                        holder.viewShade.setVisibility(View.INVISIBLE);
                    }else{
                        //设置当前图片为选中状态
                        dateListBean.setSelected(true);
                        holder.itemIvSelected.setVisibility(View.VISIBLE);
                        holder.viewShade.setVisibility(View.VISIBLE);
                    }
                }else{
                        mItemListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    static class GrowthViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_iv_growth_photo)
        RImageView itemIvGrowthPhoto;
        @BindView(R.id.item_iv_selected)
        ImageView itemIvSelected;
        @BindView(R.id.view_white_shade)
        View viewShade;
        public GrowthViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mItemListener = listener;
    }
}
